package com.bw.module_home.ui

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseActivity
import com.bw.module_home.R
import com.bw.module_home.databinding.ActivityDeviceBinding
import com.bw.module_home.device.adapter.DeviceAdapter
import com.bw.module_home.device.model.DeviceEntityItem
import com.bw.module_home.device.viewmodel.DeviceViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.Ui.DeviceActivity)
class DeviceActivity : BaseActivity<DeviceViewModel, ActivityDeviceBinding>() {
    lateinit var deviceAdapter: DeviceAdapter
    private val mViewModel: DeviceViewModel by viewModels()

    override val mLayoutResId: Int
        get() = R.layout.activity_device

    override fun initView() {
        super.initView()
        deviceAdapter = DeviceAdapter()
        mBinding.deviceRv.adapter = deviceAdapter
        mBinding.deviceRv.layoutManager = LinearLayoutManager(this)
        mViewModel.deviceList()
        mBinding.deviceBack.setOnClickListener {
            finish()
        }
        mBinding.deviceMore.setOnClickListener {
            ARouter.getInstance().build("/device/add_device").navigation()
        }
    }

    override fun initListener() {
        super.initListener()
        mViewModel.getDeviceSuccess.observe(this) { deviceEntitiesList ->
            deviceAdapter.data.clear()
            deviceEntitiesList.forEach { deviceEntities ->
                deviceAdapter.data.addAll(deviceEntities)
            }
            deviceAdapter.data.reverse()
            deviceAdapter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        mViewModel.deviceList()
    }
}